package com.dcbeida.adapter;

import android.content.Context;
import android.database.DataSetObserver;
import android.os.Build;
import android.support.v7.widget.CardView;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dcbeida.R;
import com.dcbeida.bean.MsgBean;
import com.dcbeida.utils.Utils;

import org.w3c.dom.Text;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 * 适配器
 * sean
 */
public class MsgListAdapter extends BaseAdapter {

    private ArrayList<MsgBean> mArrayList = new ArrayList<>();
    private Context mContext;
    private int lineWidth = 0;

    public MsgListAdapter(Context context) {
        super();
        this.mContext = context;
        DisplayMetrics outMetrics = new DisplayMetrics();
        ((WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(outMetrics);
        lineWidth = outMetrics.widthPixels - Utils.convertDip2Pixel(50);
    }


    @Override
    public int getCount() {
        if (mArrayList == null) {
            return 0;
        } else {
            return this.mArrayList.size();
        }
    }

    @Override
    public Object getItem(int position) {
        if (mArrayList == null) {
            return null;
        } else {
            return this.mArrayList.get(position);
        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.msg_item_new, null, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        MsgBean bean = mArrayList.get(position);
        if(bean.getReadstate() ==0){//未读
            holder.tv_read_status.setTextColor(parent.getContext().getResources().getColor(R.color.c_F64545));
            holder.tv_read_status.setText(parent.getContext().getResources().getString(R.string.msg_un_read));
            holder.cv_bg_view.setCardBackgroundColor(parent.getContext().getResources().getColor(R.color.c_E1E1E1));
        }else {
            //已读
            holder.tv_read_status.setTextColor(parent.getContext().getResources().getColor(R.color.c_979797));
            holder.tv_read_status.setText(parent.getContext().getResources().getString(R.string.msg_read));
            holder.cv_bg_view.setCardBackgroundColor(parent.getContext().getResources().getColor(R.color.ffffff));
        }

        holder.tvMsgTitle.setText(bean.getTitle());
        holder.tvMsgTime.setText(formatTime(bean.getCreatetime()));
        holder.tvMsgDetails.setText(bean.getContent());

        return convertView;
    }


    private class ViewHolder {
        LinearLayout ll_root;
        TextView tvMsgTitle;
        TextView tvMsgTime;
        TextView tvMsgDetails;
        TextView tv_read_status;
        CardView cv_bg_view;


        ViewHolder(View view) {
            ll_root = (LinearLayout) view.findViewById(R.id.ll_root);
            tvMsgTitle = (TextView) view.findViewById(R.id.tvMsgTitle);
            tvMsgTime = (TextView) view.findViewById(R.id.tvMsgTime);
            tvMsgDetails = (TextView) view.findViewById(R.id.tvMsgDetails);
            tv_read_status= (TextView) view.findViewById(R.id.tv_read_status);
            cv_bg_view= (CardView) view.findViewById(R.id.cv_bg_view);
        }

    }

    public ArrayList<MsgBean> getmArrayList() {
        return mArrayList;
    }

    public void setmArrayList(ArrayList<MsgBean> mArrayList) {
        if (mArrayList.size() >= 0) {
            this.mArrayList.addAll(mArrayList);
        }
        notifyDataSetChanged();
    }

    public void clearList() {
        this.mArrayList.clear();

    }


    private String formatTime(String time) {
        if(TextUtils.isEmpty(time))
            return "未知时间";
        String yearMonthDay = getYearMonthDay(time);//存入的时间 年-月-日
        long timeYearMonthDay = Time2Long2(yearMonthDay);//存入的时间的 年-月-日 转毫秒数

        String[] s = time.split(" ");
        String year = s[0];
        String hour = s[1].substring(0, s[1].lastIndexOf(":"));

        String nowTime = getNowTime();//当前时间 年-月-日 时:分:秒
        String yearMonthDay1 = getYearMonthDay(nowTime);//年-月-日
        long timeYearMonthDay1 = Time2Long2(yearMonthDay1);//当前时间的 年-月-日 转毫秒数

        long timeOne = timeYearMonthDay1 - timeYearMonthDay;//一天的时间差
        long oneDay = 24 * 60 * 60 * 1000;
        if (timeYearMonthDay == timeYearMonthDay1) { //小于一天 显示 12:20
            time = hour;
        } else if (timeYearMonthDay < timeYearMonthDay1 && timeOne == oneDay) {//大于一天并且小于两天 显示 昨天 加12:20
//            time = mContext.getResources().getString(R.string.yestarday) + hour;
            time = mContext.getResources().getString(R.string.yestarday);
        } else { //显示2016:01:07
            time = year;
        }

        return time;
    }

    /**
     * 2014-05-29 12:00:00
     *
     * @param time
     * @return
     */
    public String getYearMonthDay(String time) {
        SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            return sDateFormat.format(sDateFormat.parse(time));
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return time;
        }
    }

    /**
     * 字符串格式时间转成long
     */
    public long Time2Long2(String attachTime) {

        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            java.util.Date date = simpleDateFormat.parse(attachTime);
            return date.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public String getNowTime() {
        SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date curDate = new Date(System.currentTimeMillis());// 获取当前时间
        String str = sDateFormat.format(curDate);
        return str;
    }
}
